<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="50%" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:550px;overflow:auto">
                    <el-row :gutter="1">
                        <el-col :span="11">
                            <el-form-item label="模板名称" prop="mode_name">
                                <el-input v-model="form.mode_name" placeholder="模板名称" clearable autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="11">
                            <el-form-item label="合计价格" prop="total_amount">
                                <el-input v-model="form.total_amount" placeholder="合计价格" clearable autocomplete="off" readonly></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="22">
                            <sc-form-table ref="table" prop="process_data" v-model="form.process_data" :addTemplate="process" placeholder="暂无数据" drag-sort>
                                <el-table-column prop="process_id" label="一级工序">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.process_id" @change="change(scope.row)" placeholder="请选择" filterable>
                                            <el-option v-for="item in Gongxu" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="two_process_id" label="二级工序">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.two_process_id" placeholder="请选择" filterable>
                                            <el-option v-for="item in scope.row.GongxuSon" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="price" label="工价">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.price" placeholder="请输入工价" @input="input(scope.row)"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="remark" label="备注">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-col>
                    </el-row>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible = false">取 消</el-button>
                    <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增模板',
				edit: '编辑模板',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				mode_name: "",
				total_amount: "",
                process_data:[
                    // {process_id:"",price:"",remark:""}
                ]
			},
            process:{process_id:"",price:"",remark:"",two_process_id:"",GongxuSon:[]},
			//验证规则
			rules: {
				mode_name: [
					{ required: true, message: '请输入模板名称' }
				],
			},
            Gongxu:[],
		}
	},
    // computed: {
    //     priceTotal() {
    //         return this.form.process_data.reduce((total, row) => total + Number(row.price || 0), 0);
    //     }
    // },
	mounted() {
        this.getGong()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.develop.template.edit.post(this.form);
					} else {
						res = await this.$API.develop.template.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
            console.log('data :>> ', data);
			this.form.id = data.id
			this.form.mode_name = data.mode_name
			this.form.total_amount = data.total_amount
			this.form.process_data = data.detail
		},
        async getGong() {
			var res = await this.$API.all.process.get({pid:0});
			this.Gongxu = res.data
		},
        async change(e){
            var res = await this.$API.all.process.get({pid:e.process_id});
            e.GongxuSon = res.data
        },
        input(value, row) {
            console.log('value :>> ', value);
            console.log('row :>> ', row);
            let total = 0;
            for (let i = 0; i < this.form.process_data.length; i++) {
                total += parseFloat(this.form.process_data[i].price) || 0;
            }
            this.form.total_amount = total;
        }
	}
}
</script>

<style></style>
